package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Permission;
import com.pojo.Role;
import com.service.PermissionService;
import com.service.RoleService;
import com.util.ConstUtils;
import com.util.TreeNode;
import com.vo.DataVO;
import com.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/16 - 18:51
 */
@RestController
@RequestMapping("/role")
@RolesAllowed({"ROLE_ADMIN", "ROLE_SUPERADMIN"}) //JSR250权限注解 或者@Secured注解
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    //查询role集合
    @RequestMapping("/list")
    public DataVO<Role> findRoleList(RoleVo roleVo) {
        PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());
        PageInfo<Role> rolePageInfo = new PageInfo<Role>(roleService.findRoleList(roleVo));
        return new DataVO<Role>(rolePageInfo.getTotal(), rolePageInfo.getList());
    }

    //添加角色
    @RequestMapping("/add")
    public Map<String, Object> addRole(Role role) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roleService.addRole(role) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "添加成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "添加失败，请重试！");
        }
        return maps;
    }

    //修改角色
    @RequestMapping("/update")
    public Map<String, Object> updateRole(Role role) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roleService.updateRole(role) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "修改成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "修改失败，请重试！");
        }
        return maps;
    }

    //检查该角色下是否已有用户
    @RequestMapping("/checkRoleByUser")
    public Map<String, Object> checkRoleByUser(Integer roleId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roleService.checkRoleByUser(roleId) > 0) {
            maps.put(ConstUtils.EXIST, true);
            maps.put(ConstUtils.MESSAGE, "该角色下已有用户，不可删除！");
        } else {
            maps.put(ConstUtils.EXIST, false);
        }
        return maps;
    }

    //删除
    @RequestMapping("/del")
    public Map<String, Object> delRole(Integer roleId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roleService.deleteRole(roleId) > 0) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "删除成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "删除失败，请重试！");
        }
        return maps;
    }

    //给用户添加角色
    @RequestMapping("/addRoleByUserId")
    public DataVO addRoleByUserId(Integer userId) {
        //保持所有角色列表的集合
        List<Map<String, Object>> roleListByMap = roleService.findRoleListByMap();
        //保持该用户所属的角色id集合
        List<Integer> roleIdListByUserId = roleService.findRoleIdListByUserId(userId);
        for (Map<String, Object> allAoleList : roleListByMap) {
            Integer roleId = (Integer) allAoleList.get("id");
            boolean temp = false;
            for (Integer rid : roleIdListByUserId) {
                //该用户包含该角色
                if (rid == roleId) {
                    temp = true;
                    break;
                }
            }
            //将该条数据的键改为前台所需的键
            allAoleList.put("LAY_CHECKED", temp);
        }
        return new DataVO(Long.valueOf(roleListByMap.size()), roleListByMap);
    }

    //参数化角色菜单数
    @RequestMapping("/initRoleTree")
    public DataVO initRoleTree(Integer roleId) {
        List<Permission> permissions = permissionService.findPermission(null);
        List<Permission> permissionsByRoleId = permissionService.findPermissionByRoleId(roleId);
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        for (Permission permission : permissions) {
            //是否选中 0未选中 1选中
            String checkArr = "0";
            for (Permission checkPermission : permissionsByRoleId) {
                if (permission.getId() == checkPermission.getId()) {
                    checkArr = "1";
                    break;
                }
            }
            //如果该权限的spread为1则页面中的该节点为展开状态
            boolean spread = permission.getSpread() == 1;
            treeNodes.add(new TreeNode(permission.getId(), permission.getPid(), permission.getTitle(), spread, checkArr));
        }
        return new DataVO<TreeNode>(null, treeNodes);
    }

    /**
     * 给角色添加菜单
     */
    @RequestMapping("/addRolePermission")
    public Map<String, Object> addRolePermission(String ids, Integer roleId) {
        Map<String, Object> maps = new HashMap<String, Object>();
        if (roleService.addRolePermission(ids, roleId)) {
            maps.put(ConstUtils.SUCCESS, true);
            maps.put(ConstUtils.MESSAGE, "分配角色成功！");
        } else {
            maps.put(ConstUtils.SUCCESS, false);
            maps.put(ConstUtils.MESSAGE, "分配角色失败，请重试！");
        }
        return maps;
    }
}
